package com.shigu.user.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.account.exception.UtilsAccountException;
import com.shigu.component.enums.SessionEnum;
import com.shigu.exceptions.WapException;
import com.shigu.trade.bo.*;
import com.shigu.trade.service.TradeService;
import com.shigu.trade.service.TradeWapService;
import com.shigu.trade.vo.*;
import com.shigu.user.service.MallTokenService;
import com.shigu.weixin.vo.WxAppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author 郭尔
 * @description 这里的获取前session的用户都用
 *  WxAppUser wxAppUser= (WxAppUser) session.getAttribute(SessionEnum.WX_USER.toString());
 * @date 2019/1/20 18:15
 */

@Controller
@RequestMapping("wapsjson")
public class TradeWapController {
    @Autowired
    TradeWapService tradeWapService;
    @Autowired
    TradeService tradeService;
    @Autowired
    MallTokenService mallTokenService;

    @RequestMapping("tradeReceiveDetail")
    @ResponseBody
    public TradeReceiveResponse tradeReceiveDetail(@Valid TradeCodeRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeWapService.tradeReceiveDetail(request.getTradeCode());
    }

    @RequestMapping("tradeOrderDetail")
    @ResponseBody
    public TradeOrderResponse tradeOrderDetail(@Valid TradeDetialRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeWapService.tradeOrderDetail(request.getTradeCode(),request.getVersion(),request.getOs());

    }

    @RequestMapping("tradeOrderList")
    @ResponseBody
    public PageResponse<TradeOrder> tradeOrderList(TradeOrderListRequest request) {
       Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeWapService.tradeOrderList(userId, request.getPage(), request.getSize(), request.getStatus());
    }

    @RequestMapping("confirmTrade")
    @ResponseBody
    public ConfirmTradeResponse confirmTrade(ConfirmTradeRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.confirmTrade(request.getNum(), request.getItemId(), request.getSkuId(), userId,request.getVersion(),request.getOs());
    }

    @RequestMapping("tradePostFee")
    @ResponseBody
    public PostFeeResponse tradePostFee(@Valid TradePostFeeRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeService.tradePostFee(request.getTradeCode(), request.getAddressId());
    }

    @RequestMapping("receiveTrade")
    @ResponseBody
    public MallResponse receiveTrade(@Valid TradeCodeRequest request, BindingResult result) throws WapException, UtilsAccountException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeService.receiveTrade(request.getTradeCode());
    }

    @RequestMapping("goPayByWx")
    @ResponseBody
    public PayByWxResponse goPayByWx(@Valid TradeCodeRequest request, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.goPayByWx(request.getTradeCode(), userId);
    }

    @RequestMapping("appWxPay")
    @ResponseBody
    public AppWxPayResponse appWxPay(@Valid TradeCodeRequest request, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.appWxPay(request.getTradeCode(), userId);
    }

    @RequestMapping("goPayByWallet")
    @ResponseBody
    public synchronized MallResponse goPayByWallet(@Valid WalletPayRequest request, BindingResult result) throws WapException, UtilsAccountException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.goPayByWallet(request.getTradeCode(), userId, request.getPayPwd());
    }

    @RequestMapping("goPayByVoucher")
    @ResponseBody
    public synchronized MallResponse goPayByVoucher(@Valid WalletPayRequest request, BindingResult result) throws WapException, UtilsAccountException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.goPayByVoucher(request.getTradeCode(), userId, request.getPayPwd());
    }

    @RequestMapping("compeleteOrderInfo")
    @ResponseBody
    public synchronized MallResponse compeleteOrderInfo(@Valid GoPayRequest request, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return tradeService.compeleteOrderInfo(request.getAddressId(),request.getTradeCode(),userId);
    }

    @RequestMapping("giveUpTrade")
    @ResponseBody
    public MallResponse giveUpTrade(@Valid TradeCodeRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeService.giveUpTrade(request.getTradeCode());
    }

    @RequestMapping("tradeLogistics")
    @ResponseBody
    public TradeLogisticsResponse tradeLogistics(@Valid TradeCodeRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return tradeWapService.tradeLogistics(request.getTradeCode());
    }
}
